<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="while # while read line ; do echo $line ; done &lt; file.txt while read line do echo $line done &lt; file.txt # at file.txt | while read line ; do echo $line ; done cat file.txt | while read line do echo $line done for *遍历文件中的每一行  【注意】每一行中不能有空格，遍历的元素实际上是按照空格分割的
 # for line in `cat file.txt`; do echo $line; done for line in `cat /tmp/file.txt` do echo $line done 遍历字符串  可能有兼容性问题"><meta property="og:title" content="for 循环语句" />
<meta property="og:description" content="while # while read line ; do echo $line ; done &lt; file.txt while read line do echo $line done &lt; file.txt # at file.txt | while read line ; do echo $line ; done cat file.txt | while read line do echo $line done for *遍历文件中的每一行  【注意】每一行中不能有空格，遍历的元素实际上是按照空格分割的
 # for line in `cat file.txt`; do echo $line; done for line in `cat /tmp/file.txt` do echo $line done 遍历字符串  可能有兼容性问题" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://hello-world-example.github.io/linux/docs/100_Shell/030.for/" />
<meta property="article:modified_time" content="2022-12-11T01:38:37+08:00" />
<title>for 循环语句 | Linux</title>
<link rel="icon" href="/linux/favicon.png" type="image/x-icon">


<link rel="stylesheet" href="/linux/book.min.00b8e784201abfe629a6e0741e94bf44575af8612aec171d94e4ecbd3692cf5c.css" integrity="sha256-ALjnhCAav&#43;YppuB0HpS/RFda&#43;GEq7BcdlOTsvTaSz1w=">


<!--
Made with Book Theme
https://github.com/alex-shpak/hugo-book
-->

  
</head>

<body>
  <input type="checkbox" class="hidden" id="menu-control" />
  <main class="container flex">
    <aside class="book-menu">
      
  <nav>
<h2 class="book-brand">
  <a href="/linux"><span>Linux</span>
  </a>
</h2>












  

  
  





 
  
    




  
  <ul>
    
      
        

  <li>
    

  
  <a href="/linux/docs/100_Shell/" class="collapsed ">Shell</a>
  


    




  
  <ul>
    
      
        <li>

  
  <a href="/linux/docs/100_Shell/007.zsh/" class="">zsh</a>
  

</li>
      
    
      
        <li>

  
  <a href="/linux/docs/100_Shell/010.var/" class="">var 变量与参数</a>
  

</li>
      
    
      
        <li>

  
  <a href="/linux/docs/100_Shell/011.var_string/" class="">var 字符串处理</a>
  

</li>
      
    
      
        <li>

  
  <a href="/linux/docs/100_Shell/020.if/" class="">if 条件语句</a>
  

</li>
      
    
      
        <li>

  
  <a href="/linux/docs/100_Shell/030.for/" class="active">for 循环语句</a>
  

</li>
      
    
      
        <li>

  
  <a href="/linux/docs/100_Shell/035.array/" class="">数组操作</a>
  

</li>
      
    
      
        <li>

  
  <a href="/linux/docs/100_Shell/050.function/" class="">function 函数</a>
  

</li>
      
    
      
        <li>

  
  <a href="/linux/docs/100_Shell/900.21/" class="">输出从定向</a>
  

</li>
      
    
      
        <li>

  
  <a href="/linux/docs/100_Shell/901.nohup/" class="">后台运行</a>
  

</li>
      
    
  </ul>
  



  </li>


      
    
      
        

  <li>
    

  
  <a href="/linux/docs/200_Command/" class="collapsed ">Command</a>
  


    






  </li>


      
    
      
        

  <li>
    

  
  <a href="/linux/docs/300_Configuration/" class="collapsed ">文件和配置</a>
  


    






  </li>


      
    
      
        

  <li>
    

  
  <a href="/linux/docs/900_FAQ/" class="">FAQ</a>
  


    




  
  <ul>
    
  </ul>
  



  </li>


      
    
      
        

  <li>
    

  
  <a href="/linux/docs/999_Links/" class="">Links 🔗</a>
  


    




  
  <ul>
    
  </ul>
  



  </li>


      
    
  </ul>
  



  
















</nav>




  <script>(function(){var menu=document.querySelector("aside.book-menu nav");addEventListener("beforeunload",function(event){localStorage.setItem("menu.scrollTop",menu.scrollTop);});menu.scrollTop=localStorage.getItem("menu.scrollTop");})();</script>


 
    </aside>

    <div class="book-page">
      <header class="book-header">
        
  <div class="flex align-center justify-between">
  <label for="menu-control">
    <img src="/linux/svg/menu.svg" class="book-icon" alt="Menu" />
  </label>

  <strong>for 循环语句</strong>

  <label for="toc-control">
    <img src="/linux/svg/toc.svg" class="book-icon" alt="Table of Contents" />
  </label>
</div>


  
    <input type="checkbox" class="hidden" id="toc-control" />
    <aside class="hidden clearfix">
      
  <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#while">while</a></li>
        <li><a href="#for">for</a>
          <ul>
            <li><a href="#遍历文件中的每一行">*遍历文件中的每一行</a></li>
            <li><a href="#遍历字符串">遍历字符串</a></li>
            <li><a href="#遍历当前文件夹">遍历当前文件夹</a></li>
            <li><a href="#1-10-乘以4-输出">1-10 乘以4 输出</a></li>
            <li><a href="#序列输出">序列输出</a></li>
            <li><a href="#100-以内5的倍数">100 以内5的倍数</a></li>
          </ul>
        </li>
        <li><a href="#read-more">Read More</a></li>
      </ul>
    </li>
  </ul>
</nav>


    </aside>
  
 
      </header>

      
      
  <article class="markdown"><h2 id="while">while</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#75715e"># while read line ; do echo $line ; done &lt; file.txt</span>

<span style="color:#66d9ef">while</span> read line
<span style="color:#66d9ef">do</span>
    echo $line
<span style="color:#66d9ef">done</span> &lt; file.txt
</code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#75715e"># at file.txt | while read line ; do echo $line ; done</span>

cat file.txt | <span style="color:#66d9ef">while</span> read line
<span style="color:#66d9ef">do</span>
    echo $line
<span style="color:#66d9ef">done</span>
</code></pre></div><h2 id="for">for</h2>
<h3 id="遍历文件中的每一行">*遍历文件中的每一行</h3>
<blockquote>
<p>【注意】每一行中不能有空格，遍历的元素实际上是按照空格分割的</p>
</blockquote>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#75715e"># for line in `cat file.txt`; do echo $line; done</span>

<span style="color:#66d9ef">for</span> line in <span style="color:#e6db74">`</span>cat /tmp/file.txt<span style="color:#e6db74">`</span>
<span style="color:#66d9ef">do</span>
    echo $line
<span style="color:#66d9ef">done</span>
</code></pre></div><h3 id="遍历字符串">遍历字符串</h3>
<blockquote>
<p>可能有兼容性问题</p>
</blockquote>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">strs<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;1&#34;</span>
strs<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span>$strs<span style="color:#e6db74"> 22&#34;</span>
strs<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span>$strs<span style="color:#e6db74"> 333&#34;</span>
strs<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span>$strs<span style="color:#e6db74"> 4444&#34;</span>

<span style="color:#66d9ef">for</span> str in <span style="color:#e6db74">${</span>strs[@]<span style="color:#e6db74">}</span>
<span style="color:#66d9ef">do</span>
    echo <span style="color:#e6db74">${</span>str<span style="color:#e6db74">}</span>;
<span style="color:#66d9ef">done</span>
</code></pre></div><h3 id="遍历当前文件夹">遍历当前文件夹</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#66d9ef">for</span> i in <span style="color:#e6db74">`</span>ls<span style="color:#e6db74">`</span>
<span style="color:#66d9ef">do</span> 
    echo $i
<span style="color:#66d9ef">done</span>
</code></pre></div><h3 id="1-10-乘以4-输出">1-10 乘以4 输出</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#75715e"># for((i=1;i&lt;=10;i++));do echo $(expr $i \* 4);done</span>

<span style="color:#66d9ef">for</span><span style="color:#f92672">((</span>i<span style="color:#f92672">=</span>1; i&lt;<span style="color:#f92672">=</span>10; i++<span style="color:#f92672">))</span>;
<span style="color:#66d9ef">do</span> 
    echo <span style="color:#66d9ef">$(</span>expr $i <span style="color:#ae81ff">\*</span> 4<span style="color:#66d9ef">)</span>;
<span style="color:#66d9ef">done</span>
</code></pre></div><h3 id="序列输出">序列输出</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#66d9ef">for</span> i in <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">4</span> <span style="color:#ae81ff">5</span>
<span style="color:#66d9ef">do</span> 
    echo $i;
<span style="color:#66d9ef">done</span>
</code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#66d9ef">for</span> i in <span style="color:#e6db74">`</span>seq <span style="color:#ae81ff">1</span> 5<span style="color:#e6db74">`</span>
<span style="color:#66d9ef">do</span> 
    echo $i;
<span style="color:#66d9ef">done</span>
</code></pre></div><h3 id="100-以内5的倍数">100 以内5的倍数</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#66d9ef">for</span> i in <span style="color:#e6db74">`</span>seq 100<span style="color:#e6db74">`</span>
<span style="color:#66d9ef">do</span> 
    <span style="color:#66d9ef">if</span><span style="color:#f92672">((</span>i%5<span style="color:#f92672">==</span>0<span style="color:#f92672">))</span>
    <span style="color:#66d9ef">then</span>
        echo $i
        <span style="color:#66d9ef">continue</span>
    <span style="color:#66d9ef">fi</span>
<span style="color:#66d9ef">done</span>
</code></pre></div><h2 id="read-more">Read More</h2>
<ul>
<li>
  <a href="https://www.cnblogs.com/iloveyoucc/archive/2012/07/10/2585529.html">shell:读取文件的每一行内容并输出</a></li>
</ul>
</article>
 
      

      <footer class="book-footer">
        
  <div class="flex justify-between">



  <div>
    
    <a class="flex align-center" href="https://github.com/hello-world-example/Linux/commit/d07f642a1d3d369f2ee692c45224763cdf4a7f29" title='Last modified by kaibin.yang | Dec 11, 2022' target="_blank" rel="noopener">
      <img src="/linux/svg/calendar.svg" class="book-icon" alt="Calendar" />
      <span>Dec 11, 2022</span>
    </a>
  </div>



  <div>
    <a class="flex align-center" href="https://github.com/hello-world-example/Linux/edit/master/HuGo/content/docs/100_Shell/030.for.md" target="_blank" rel="noopener">
      <img src="/linux/svg/edit.svg" class="book-icon" alt="Edit" />
      <span>Edit this page</span>
    </a>
  </div>

</div>

 
        <script>
    var images = document.getElementsByTagName("img")
    for (var i = 0; i < images.length; i++) {
        var image = images[i]
        var src = image.getAttribute("src");
         
        if (src.startsWith("-images")) {
            image.setAttribute("src", "../" + src)
        }
         
        if (window.location.href.indexOf("/posts/")) {
            src = src.replace("../drawio/", "../../drawio/")
            image.setAttribute("src", src)
        }
         
        if (src.startsWith("../drawio/")) {
            image.setAttribute("src", src.replace("../drawio/", "../"))
        }

         
        if(src.indexOf("/svg/") < 0){
             
            image.onclick = function (e) {
                window.open(e.target.getAttribute("src"))
            }
        }
    }

</script>

      </footer>

      
  
  <div class="book-comments">

</div>
  
 

      <label for="menu-control" class="hidden book-menu-overlay"></label>
    </div>

    
    <aside class="book-toc">
      
  <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#while">while</a></li>
        <li><a href="#for">for</a>
          <ul>
            <li><a href="#遍历文件中的每一行">*遍历文件中的每一行</a></li>
            <li><a href="#遍历字符串">遍历字符串</a></li>
            <li><a href="#遍历当前文件夹">遍历当前文件夹</a></li>
            <li><a href="#1-10-乘以4-输出">1-10 乘以4 输出</a></li>
            <li><a href="#序列输出">序列输出</a></li>
            <li><a href="#100-以内5的倍数">100 以内5的倍数</a></li>
          </ul>
        </li>
        <li><a href="#read-more">Read More</a></li>
      </ul>
    </li>
  </ul>
</nav>

 
    </aside>
    
  </main>

  
</body>

</html>












